package com.leo.gateway//package com.leo.gateway
//
//import com.alibaba.fastjson.JSONObject
//import com.alibaba.nacos.api.NacosFactory
//import com.alibaba.nacos.api.PropertyKeyConst
//import com.alibaba.nacos.api.config.ConfigService
//import com.alibaba.nacos.api.config.listener.Listener
//import com.alibaba.nacos.api.exception.NacosException
//import org.slf4j.Logger
//import org.slf4j.LoggerFactory
//import org.springframework.beans.factory.annotation.Autowired
//import org.springframework.cloud.gateway.event.RefreshRoutesEvent
//import org.springframework.cloud.gateway.route.RouteDefinition
//import org.springframework.cloud.gateway.route.RouteDefinitionWriter
//import org.springframework.context.ApplicationEventPublisher
//import org.springframework.context.ApplicationEventPublisherAware
//import org.springframework.context.annotation.Bean
//import org.springframework.stereotype.Service
//import org.springframework.web.util.UriComponentsBuilder
//import reactor.core.publisher.Mono
//import java.util.*
//import java.util.concurrent.Executor
//
//@Service
//class DynamicRoutingConfig :ApplicationEventPublisherAware {
//    val logger : Logger = LoggerFactory.getLogger(DynamicRoutingConfig::class.java)
//    val DATA_ID = "gateway-refresh-dev.json"
//    val Group = "DEFAULT_GROUP"
//
//    @Autowired
//    private lateinit var routeDefinitionWriter: RouteDefinitionWriter
//
//    lateinit var eventPublisher:ApplicationEventPublisher
//
//    override fun setApplicationEventPublisher(applicationEventPublisher: ApplicationEventPublisher) {
//        eventPublisher = applicationEventPublisher
//    }
//
//    @Bean
//    @Throws(NacosException::class)
//    fun refreshRouting() {
//        val properties = Properties()
//        properties[PropertyKeyConst.SERVER_ADDR] = "111.231.121.142:8848"
////        properties[PropertyKeyConst.NAMESPACE] = "8282c713-da90-486a-8438-2a5a212ef44f"
//        val configService: ConfigService = NacosFactory.createConfigService(properties)
//        configService.addListener(DATA_ID, Group, object : Listener{
//            override fun getExecutor(): Executor? {
//                return null
//            }
//            override fun receiveConfigInfo(configInfo: String?) {
//                logger.info("开始配置")
//                configInfo?.let {
//                    logger.info(configInfo)
//                    val refreshGateway = JSONObject.parseObject(configInfo).getBoolean("refreshGatewayRoute")
//                    if (refreshGateway){
//                        val list = JSONObject.parseArray(JSONObject.parseObject(configInfo).getString("routeList")).toJavaList(RouteDefinition::class.java)
//                        for (route in list){
//                            update(route)
//                        }
//                    }else{
//                        logger.info("路由未发生变更")
//                    }
//                }
//            }
//        })
//    }
//
//    private fun update(routeDefinition: RouteDefinition){
//        try {
//            this.routeDefinitionWriter.delete(Mono.just(routeDefinition.id))
//            logger.info("路由删除成功")
//        } catch (e: Exception) {
//            logger.error(e.message, e)
//        }
//        try {
//            this.routeDefinitionWriter.save(Mono.just(routeDefinition)) .subscribe()
//            eventPublisher.publishEvent(RefreshRoutesEvent(this))
//            logger.info("路由更新成功")
//        } catch (e: Exception) {
//            logger.error(e.message, e)
//        }
//    }
//}